/**
滑动验证码
**/
var NKSlide = Class.create({
   version: '1.0',
   //--------------参数初始化-------------------------------------------------------
   initialize : function(dbModel) {
        this.id = dbModel.id;//生成滑动验证的id
        this.rurl=dbModel.rurl;//根路径
        this.width=dbModel.width;
        this.name =dbModel.name;
        this.v1=dbModel.v1;
        this.v2=dbModel.v2;
        this.height=30;
        this.isload = false;//是否加载完
        this.ischedked = false;//是否已验证
   },
   //--------------控件件初始化------------------------------------------------------
   show : function(){
        var self = this;
        self.createHtml();
        self.ajaxRequest();
        self.EventSlide();
   },
   //-------------创建滑动界面-------------------------------------------------------
   createHtml:function(){
        var self = this;
        var idx=0;
        var html=[];
        html[idx++]='<div id="'+self.id+'_button"  style="cursor: pointer;border: solid 1px #cccccc;width:40px;height:30px;background:#ffffff url('+self.rurl+'/nokatag/org/noka/bver/rt.png) no-repeat 11px 7px;z-index:9999;position:absolute;">&nbsp;</div>';
        html[idx++]='<div id="'+self.id+'_img"  style="background-color:#14bbf9;border:1px solid #cccccc;font-size:12;color:#FFFFFF;z-index:1;padding-left:70px;line-height:'+(self.height)+'px;overflow: hidden;height:'+(self.height)+'px;">'+self.v1+'</div>';
        html[idx++]='<input name="'+self.name+'"      id="'+self.id+'" value="" type="hidden">';
        html[idx++]='<input name="'+self.name+'_cr"   id="'+self.id+'_cr" value="" type="hidden">';
        html[idx++]='<input name="'+self.name+'_icr"  id="'+self.id+'_icr" value="" type="hidden">';
        $(self.id+'_div').innerHTML=html.join('');
   },
   ajaxRequest:function(){
	   var self = this;
	   var rurl = self.rurl+'/nokatag/bevcode.codebver?ac='+new Date();
	   new Ajax.Request(rurl, {
           parameters: 'w='+self.width+'&h='+self.height,
           method:'post',
           onSuccess: function(response) {
        	   var tv = response.responseText.evalJSON();
        	   $(self.id+'_cr').value=tv.cr;
        	   $(self.id+'_icr').value=tv.icr;
        	   self.isload = true;
           },
           onFailure : function(transport) {
           }
       	});
   },
   ajaxChek : function(){//
	   var self = this;
	   $(self.id+'_img').innerHTML='Loading.....';
	   self.iscload = true;
	   self.ischedked=true;//已经验证
	   var rurl = self.rurl+'/nokatag/bevcode.codebverjson?ac='+new Date();
	   new Ajax.Request(rurl, {
           parameters: 'f='+$(self.id).value+'&f_cr='+$(self.id+'_cr').value+'&f_icr='+$(self.id+'_icr').value,
           method:'post',
           onSuccess: function(response) {
        	   var tv = response.responseText.evalJSON();
        	   if(tv.c){//验证成功
		           $(self.id+'_img').setStyle({backgroundColor:'#76c61d',borderColor:'#cccccc',paddingleft:'2px',paddingright:'100px'});
		           $(self.id+'_img').innerHTML=self.v2;
		           $(self.id+'_button').setStyle({backgroundImage:'url('+self.rurl+'/nokatag/org/noka/bver/yes.png)'});
		           self.ischedked=true;//已经验证
        	   }else{
        		   $(self.id).reload();
        		   self.ischedked=false;
        	   }
           },
           onFailure : function(transport) {
        	   self.ischedked=false;
           }
       	});
   },
   //-------------滑动方法---------------------------------------------------------
   EventSlide : function(){
        var self = this;
        var sbutton = $(self.id+'_button');
        var sbody = $(self.id+'_div');
        var idx=0;
        var hv = [];
        self.activeDraggable=false;
        self.Lcd = 0;//位置差
        self.BSLeft = 0;//sbody.offsetLeft;
        self.initLeft = sbutton.offsetLeft;//初始位置
        self.BELeft = sbody.getWidth()-sbutton.getWidth();
        sbutton.observe('mousedown',function(event){//鼠标按下
        	if(self.isload && !self.ischedked && self.BSLeft<self.BELeft){
	        	Event.stop(event);
	        	self.activeDraggable=true;
	        	self.Lcd = Event.pointerX(event) - sbutton.offsetLeft;
	        	idx=0;
        	}
        });
        document.observe('mouseup',function(event){//鼠标弹起
        	if(!self.ischedked){
	        	if(self.isload &&(self.BSLeft>=self.BELeft) && self.activeDraggable){
		        	$(self.id).value=hv.toJSON();
		        	self.ajaxChek();
	        	}else{
	        		sbutton.initLeft();
	        	}
        	}
        	self.activeDraggable=false;
        });
        document.observe('mousemove',function(event){//鼠标移动
        	if(self.activeDraggable && self.isload && !self.ischedked){
        		try{
        			Event.stop(event);
        			var mx = Event.pointerX(event);
        			var my = Event.pointerY(event)-sbody.offsetTop;
        			var left = mx- self.Lcd;
        			if(self.BSLeft<=left){//只能向右滑动
	        			if(sbutton.offsetLeft<self.BELeft){
		        			self.BSLeft = left;
	        				sbutton.setStyle({left:left+'px'});
	        				hv[idx++]={x:left,y:my};
	        				$(self.id+'_img').setStyle({backgroundImage:'url('+self.rurl+'/nokatag/org/noka/bver/bck.png)',backgroundRepeat:'no-repeat',backgroundPosition:(self.BSLeft-800)+'px 0px'});
	        			}
	        			if(sbutton.offsetLeft>self.BELeft){
	        				sbutton.setStyle({left:self.BELeft+'px'});
	        			}
        			}
       			}catch(e){}
       		}
       	});
        //-----------------回到初始位置----------------------------------------
        sbutton.initLeft = function(){
        	self.activeDraggable=false;
        	self.ischedked=false;
        	sbutton.setStyle({left:self.initLeft+'px'});
        	self.BSLeft=0;
        	$(self.id+'_img').setStyle({backgroundColor:'#14bbf9',borderColor:'#cccccc',paddingleft:'70px',paddingright:'2px',backgroundImage:'url()'});
        	$(self.id+'_img').innerHTML=self.v1;
        	$(self.id+'_button').setStyle({backgroundImage:'url('+self.rurl+'/nokatag/org/noka/bver/rt.png)'});
        };
        //-----------------刷新---------------------------------------------
        $(self.id).reload = function(){
        	self.isload=false;
        	self.ajaxRequest();
        	sbutton.initLeft();
        };
      }
});